/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.telas;

import java.util.List;
import javax.swing.JOptionPane;
import sys.donto.servico.Servico;
import sys.donto.servico.ServicoDAO;
import sys.donto.servico.ServicoTableModel;
import sys.donto.util.Util;

/**
 *
 * @author JOABB
 */
public class telaServico extends javax.swing.JDialog {
    private Servico servico;
    /**
     * Creates new form telaServico
     */
    public telaServico() { //METODO CONSTRUTOR
        initComponents();
        setModal(true);
        setLocationRelativeTo(null);
        limpaCampos();
        
        setTitle("sysDONTO - Cadastro de Serviços"); //Define nome no titulo da tela
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        tfServico = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfValor = new javax.swing.JFormattedTextField();
        btSair1 = new javax.swing.JButton();
        btNovo1 = new javax.swing.JButton();
        btSalvar1 = new javax.swing.JButton();
        btPesquisar1 = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(null);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Descrição:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(30, 140, 61, 15);

        tfServico.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfServicoActionPerformed(evt);
            }
        });
        jPanel1.add(tfServico);
        tfServico.setBounds(100, 140, 550, 20);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Valor (R$):");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(30, 170, 66, 15);

        tfValor.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        jPanel1.add(tfValor);
        tfValor.setBounds(100, 170, 90, 20);

        btSair1.setBackground(new java.awt.Color(0, 153, 153));
        btSair1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSair1.setForeground(new java.awt.Color(255, 255, 255));
        btSair1.setMnemonic('S');
        btSair1.setText("SAIR");
        btSair1.setToolTipText("Sair");
        btSair1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSair1ActionPerformed(evt);
            }
        });
        jPanel1.add(btSair1);
        btSair1.setBounds(590, 230, 63, 30);

        btNovo1.setBackground(new java.awt.Color(0, 153, 153));
        btNovo1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btNovo1.setForeground(new java.awt.Color(255, 255, 255));
        btNovo1.setMnemonic('N');
        btNovo1.setText("NOVO");
        btNovo1.setToolTipText("Novo");
        btNovo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovo1ActionPerformed(evt);
            }
        });
        jPanel1.add(btNovo1);
        btNovo1.setBounds(220, 230, 70, 30);

        btSalvar1.setBackground(new java.awt.Color(0, 153, 153));
        btSalvar1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSalvar1.setForeground(new java.awt.Color(255, 255, 255));
        btSalvar1.setMnemonic('S');
        btSalvar1.setText("SALVAR");
        btSalvar1.setToolTipText("Salvar");
        btSalvar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btSalvar1);
        btSalvar1.setBounds(300, 230, 83, 30);

        btPesquisar1.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisar1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btPesquisar1.setForeground(new java.awt.Color(255, 255, 255));
        btPesquisar1.setMnemonic('P');
        btPesquisar1.setText("PESQUISAR");
        btPesquisar1.setToolTipText("Pesquisar");
        btPesquisar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btPesquisar1);
        btPesquisar1.setBounds(390, 230, 103, 30);

        btExcluir.setBackground(new java.awt.Color(0, 153, 153));
        btExcluir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btExcluir.setForeground(new java.awt.Color(255, 255, 255));
        btExcluir.setMnemonic('E');
        btExcluir.setText("EXCLUIR");
        btExcluir.setToolTipText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });
        jPanel1.add(btExcluir);
        btExcluir.setBounds(500, 230, 85, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("CADASTRO DE SERVIÇO");
        jLabel3.setFocusable(false);
        jPanel1.add(jLabel3);
        jLabel3.setBounds(210, 40, 360, 40);

        jLFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/telasPP.jpg"))); // NOI18N
        jPanel1.add(jLFundo);
        jLFundo.setBounds(0, 0, 740, 320);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limpaCampos(){
        tfServico.setText("");
        tfValor.setText("");
        servico = new Servico();
        btExcluir.setEnabled(false);
    }
    private void tfServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfServicoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfServicoActionPerformed

    private void btSair1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSair1ActionPerformed
        dispose();
    }//GEN-LAST:event_btSair1ActionPerformed

    private void btNovo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovo1ActionPerformed
        limpaCampos(); //CHAMA METODO PARA LIMPAR CAMPOS
    }//GEN-LAST:event_btNovo1ActionPerformed

    private void btSalvar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvar1ActionPerformed
        if (Util.chkVazio(tfServico.getText(),tfValor.getText())){//Condição para verificação se campos estão vzios.

            servico.setDescricao(tfServico.getText());
            servico.setValor(Double.valueOf(tfValor.getText().replaceAll(",",".")));  

            ServicoDAO dao = new ServicoDAO();
            dao.salvar(servico);// chama o metodo salvar da classe cidade.
            limpaCampos();

            JOptionPane.showMessageDialog(rootPane, "Cadastro de Servico Efetuado com Sucesso!");
        }
    }//GEN-LAST:event_btSalvar1ActionPerformed

    private void btPesquisar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisar1ActionPerformed
        ServicoDAO dao = new ServicoDAO();
        List <Servico> servicos = (!tfServico.getText().isEmpty() ? dao.pesquisaDescricao(tfServico.getText())
            : dao.listar()); //descrição que diz que se o campo descrição estiver vazio ele pega a descrição digitada.

        ServicoTableModel stm = new ServicoTableModel(servicos);

        Object o = telaPesquisa.exibeTela(stm, "Servico");

        if (o != null){
            servico = new Servico();
            servico = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));

            tfServico.setText(servico.getDescricao());
            tfValor.setText(String.valueOf(servico.getValor()));

            btExcluir.setEnabled(true);
        }
    }//GEN-LAST:event_btPesquisar1ActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        if (JOptionPane.showConfirmDialog(rootPane,
            "Deseja excluir Serviço?",
            "Excluir?", JOptionPane.YES_NO_OPTION)
        == JOptionPane.YES_OPTION) {

        }
        ServicoDAO dao = new ServicoDAO();
        dao.remover(servico);//metodopara remover, excluir.
        limpaCampos();

        JOptionPane.showMessageDialog(rootPane, "Servico Excluído com Sucesso!");
    }//GEN-LAST:event_btExcluirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaServico().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo1;
    private javax.swing.JButton btPesquisar1;
    private javax.swing.JButton btSair1;
    private javax.swing.JButton btSalvar1;
    private javax.swing.JLabel jLFundo;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tfServico;
    private javax.swing.JFormattedTextField tfValor;
    // End of variables declaration//GEN-END:variables
}
